package com.four.user.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.four.user.service.UserService;
import com.four.user.service.impl.UserServiceImpl;

@WebServlet("/LoginServlet.do")
public class LoginServlet extends HttpServlet{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		this.doPost(req, resp);//调用自己的doPost方法
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		//1、设置请求对象中的编码格式
		req.setCharacterEncoding("UTF-8");
		//2、获取传入的账号及密码
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		UserService service = new UserServiceImpl();
		boolean result = service.login(username, password);
		if(result) {
			//如果有数据则转发首页
			req.getRequestDispatcher("view/index.html").forward(req, resp);
		}else {
			//如果没有数据则重定向到注册页面
			resp.sendRedirect("view/register.html");
		}
	}
	

}
